<html>
<head><meta charset="utf-8"><title>Extract callgraph of a crate · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html">Extract callgraph of a crate</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216012557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012557">(Nov 08 2020 at 15:57)</a>:</h4>
<p>Hello, hello!<br>
In order to have a good understanding of an unknown codebase, I would like to be able to interactively visualize the whole call graph. I searched before, and didn't found one. But since I have some free time, I could build it myself! The pipeline is relatively simple:</p>
<ul>
<li>extract the fully qualified name of all functions/methods declared in the crate.</li>
<li>create an hyperlink to the documentation of said function.</li>
<li>extract from the AST/IR/…? the fully qualified name of all called functions. Ideally also extract all Fn, Trait parameters and other means to inject function dependencies<ul>
<li>create a graph of dependencies, export it with graphviz</li>
<li>displays the graph</li>
</ul>
</li>
</ul>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> told me that I could write a <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/visit/trait.Visitor.html"><code>Visitor</code></a> that only looks at functions. He also told me that I can take a look at <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/util/struct.ReplaceBodyWithLoop.html"><code>ReplaceBodyWithLoop</code></a> as an example. More specifically, apparently what I need is <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.body_owners"><code> tcx.hir().krate().body_ids</code></a> to be able to look at each function body one at at time. </p>
<p>My question is: how do I write my <code>main()</code>? More specifically, how do I build that <code>tcx</code>?</p>



<a name="216012702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012702">(Nov 08 2020 at 16:01)</a>:</h4>
<p><span class="user-mention" data-user-id="281841">@Robin Moussu</span> sorry, those were instructions for rustc, not rust-analyzer</p>



<a name="216012704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012704">(Nov 08 2020 at 16:01)</a>:</h4>
<p>I don't know how rust-analyzer works</p>



<a name="216012789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012789">(Nov 08 2020 at 16:03)</a>:</h4>
<p>You are right, I messed-up the acronyms in my head. Where should I move it?</p>



<a name="216012839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012839">(Nov 08 2020 at 16:04)</a>:</h4>
<p>well it depends if you want to use r-a or not <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="216012852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012852">(Nov 08 2020 at 16:05)</a>:</h4>
<p>Given that you started to give me some good pointer, I will go with rustc!</p>



<a name="216012863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012863">(Nov 08 2020 at 16:05)</a>:</h4>
<p>see <a href="https://github.com/rust-lang/rust-clippy/blob/master/src/driver.rs#L418-L422">https://github.com/rust-lang/rust-clippy/blob/master/src/driver.rs#L418-L422</a> for examples of how to use rustc_driver then</p>



<a name="216012865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012865">(Nov 08 2020 at 16:05)</a>:</h4>
<p>rustdoc overcomplicates this, don't look there</p>



<a name="216012914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012914">(Nov 08 2020 at 16:06)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281841">Robin Moussu</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Extract.20callgraph.20of.20a.20crate/near/216012789">said</a>:</p>
<blockquote>
<p>You are right, I messed-up the acronyms in my head. Where should I move it?</p>
</blockquote>
<p><a class="stream" data-stream-id="182449" href="/#narrow/stream/182449-t-compiler.2Fhelp">#t-compiler/help</a> is a good place</p>



<a name="216012996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216012996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216012996">(Nov 08 2020 at 16:09)</a>:</h4>
<p>Moved: <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Extract.20callgraph.20of.20a.20crate">https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Extract.20callgraph.20of.20a.20crate</a></p>



<a name="216099733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Extract%20callgraph%20of%20a%20crate/near/216099733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Extract.20callgraph.20of.20a.20crate.html#216099733">(Nov 09 2020 at 15:31)</a>:</h4>
<p><code>call_hierarchy</code> does some of this</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>